<?php
class Maverick_AuthPlugin extends Zend_Controller_Plugin_Abstract
{
    protected $_auth;
    protected $_acl;
   
    public function __construct(Zend_Auth $auth, Zend_Acl $acl)
    {
        $this->_auth = $auth;
        $this->_acl = $acl;
        $this->_auth->setStorage(new Zend_Auth_Storage_Session('dupa2'));
    }
    /**
     * Enter description here...
     *
     * @return Zend_Acl
     */
    public function getAcl(){
    	return $this->_acl;
    }
   public function preDispatch(Zend_Controller_Request_Abstract $request){
         //  d('preDispach<br>');
     
   		if ($this->_auth->hasIdentity())
        {           
            $role = $this->_auth->getStorage()->read();
          
        }
        else
        {
            $role = 'guest';
	        $this->_auth->getStorage()->write($role);
        }
        //d('plugin auth<br>');
        $controller = $request->getControllerName();
        $module = $request->getModuleName();
        $action = $request->getActionName();
        $resource = $module;
        if ($resource == 'admin'){
        } else {
        	if ($controller == 'admin'){
        		//sprawdzanie praw dostepu
        		if ($this->getAcl()->isAllowed($role,$resource)){
        			
        			
        		} else {
        			if ($action != 'upload'){
        				//zezwalamy na akcję upload ze względu na upload plikow przy wykorzystaniu swf upload
	        			$this->_request->setControllerName('user');
			    		$this->_request->setModuleName('admin');
			    		$this->_request->setActionName('denied');
        			}
        		}
        		
        	}
        }
        
        /**
         * 
        die;
        if (!$this->_acl->has($resource))
        {
            $resource = null;
        }
        
        if ($module == 'admin'){
	        if (!$this->_acl->isAllowed($role, $resource))
	        {
	            if (!$this->_auth->hasIdentity())
	            {               
	            }
	            else
	            {
	            	//d('nie udzielono pozwolenia');
	                $request->setModuleName('admin');
	                //$request->setControllerName('index');
	                $request->setActionName('denied');
	            }
	        } else {
	        	//d('<br>udzielono dostepu<br>');	
	        }
        }
         */

   	/*	
   	$this->_auth = Zend_Auth::getInstance();
    	//print_r($this->_getAllParams());
    	echo $this->_auth->user;
    	echo Zend_Auth::getIdentity();
    	
    	if (!$this->_auth->role){
    		 $this->_auth->role = 'guest';
    	}
    	if (!$this->_auth->user){
    		$this->_auth->user = 'unknown'; 
    	}
    	
    	//$acl = new Wellbet_Acl();
    	
    	$resource = $this->_request->getControllerName();
    	$module = $this->getRequest()->getModuleName();

    	if (!$this->_acl->isAllowed($this->_auth->role, $module)){
    		$this->_request->setControllerName('user');
    		$this->_request->setModuleName('admin');
    		$this->_request->setActionName('denied');
		} else {
			echo 'is allow<br>';
		}
	*/
   }
}
?>